# -*- coding: utf-8 -*-
# @Author : HJH
# @Time : 2024/1/16 17:51
# @Use :
import datetime
import concurrent.futures
import os
import sys

import pandas as pd
import requests
import hashlib
import time
import json

from loguru import logger
from tqdm import tqdm


def log_config():
    # 添加输出处理程序，将日志输出到文件
    logger.add("./logs/info_file.log", level="INFO")
    logger.add("./logs/error_file.log", level="ERROR")


def get_date_time(datetime_str):
    # 输入日期时间字符串
    # datetime_str = "2024-02-15 21:15:00"

    # 将日期时间字符串转换为datetime对象
    datetime_obj = datetime.datetime.strptime(datetime_str, "%Y-%m-%d %H:%M")

    # 获取当前时区的时间戳
    timestamp = datetime_obj.timestamp()

    # 将时间戳转换为整数
    timestamp = int(timestamp)

    # 在时间戳后面添加3个零，使其长度为13位
    return str(timestamp) + "000"


def send_request(flight_no, cabin, dep, arr, dep_time, arr_time):
    appkey = "B20230907024"
    secret = "NoPt5JpRfRU6d4pR75Mz8nWdLQafhGbR"
    url = "http://openapi.tenfli.com/api/order/policies"
    req = {
        "accounts":
            [
                {
                    "supplierCode": "OTA_FLIGGY",

                    # "username": "tb481489926298",
                    # "password": "ychk@1234",

                    "username": "tb123979180",
                    "password": "ychk@1234",

                    # "username": "tb992720552747",
                    # "password": "ychk@12345",
                }
            ],
        "keepNotMatched": True,
        "otaOrderNo": "exw240116172920466003",
        "tickets":
            [
                {
                    "arr": f"{arr}",
                    "arrTime": f"{get_date_time(arr_time)}",
                    "birthDay": "1996-01-10",
                    "buildPrice": 0,
                    "cabinCode": f"{cabin}",
                    "certCode": "410185200201189869",
                    "certType": "ID",
                    "dep": f"{dep}",
                    "depTime": f"{get_date_time(dep_time)}",
                    "flightNo": f"{flight_no}",
                    "fuelPrice": 0,
                    "gender": "FEMALE",
                    "mobile": "13783590698",
                    "name": "赵瑞凯",
                    "printPrice": 0,
                    "salePrice": 0,
                    "type": "ADU"
                }
            ]
    }

    # 签名生成
    time_millis = int(time.time() * 1000)
    text = f"{appkey}&{time_millis}&{secret}"
    sign = hashlib.md5(text.encode('utf-8')).hexdigest()

    # 构建请求
    headers = {
        "Content-Type": "application/json",
        "appKey": appkey,
        "sign": sign,
        "timestamp": str(time_millis)
    }

    """
    请求接口
    """
    response = requests.post(url, headers=headers, json=req)
    result = response.text
    res_data = json.loads(result)
    print(f"FZ: {json.dumps(res_data)}")

    mt_params = {
        'action': 'ticket/searchFlightList',
        'partnerId': 'HNYC231226742',
        'password': 'DVlM59zj1ZS3gjOOd8hMoFTuAp-vutcAfYcM_HU9efs',
        'data': {
            'depCityCode': dep,
            'arrCityCode': arr,
            'date': dep_time.split(" ")[0],
        }
    }

    mt_url = 'http://59.110.171.247:6180/services/reissue/mt/searchFlightList'
    mt_response = requests.post(url=mt_url, json=mt_params)
    mt_result = mt_response.text
    mt_res_data = json.loads(mt_result)
    print(f"MT: {json.dumps(mt_res_data)}")


if __name__ == '__main__':
    tc_rp = send_request('SC7645', 'K', 'CGQ', 'YNT', '2024-06-26 18:45', '2024-06-26 20:30')
